package org.dg.dgimg.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @Writer:DG
 * @Data:2024/6/26 15:50
 * @explain:
 */
public class JWTUtils {
    //Jwt秘钥
    private static final String key = "DGIsSuperKindAndAsksForMercy";

        //根据用户信息创建Jwt令牌
        public static String createJwt(String token){
            Algorithm algorithm = Algorithm.HMAC256(key);
            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();
            calendar.add(Calendar.SECOND, 3600 * 24 * 7); //7天有效期
            return JWT.create()
                    .withClaim("USERID", token)  //配置JWT自定义信息
//                    .withClaim("authorities", user.getAuthorities().stream().map(GrantedAuthority::getAuthority).toList())
                    .withExpiresAt(calendar.getTime())  //设置过期时间
                    .withIssuedAt(now)    //设置创建创建时间
                    .sign(algorithm);   //最终签名
        }

        //根据Jwt验证并解析用户信息
        public static String resolveJwt(String token){
            Algorithm algorithm = Algorithm.HMAC256(key);
            JWTVerifier jwtVerifier = JWT.require(algorithm).build();
            try {
                DecodedJWT verify = jwtVerifier.verify(token);  //对JWT令牌进行验证，看看是否被修改
                Map<String, Claim> claims = verify.getClaims();  //获取令牌中内容
                if(new Date().after(claims.get("exp").asDate())) //如果是过期令牌则返回null
                    return null;
                else {// 重新组装为User对象，包括用户名、授权信息等
                    // 可根据需要设置其他属性
                    // user.setPassword(""); // 设置密码，如果需要的话
                    // user.setAuthorities(claims.get("authorities").asArray(String.class)); // 设置授权信息，如果需要的话
                    return claims.get("USERID").asString();
                }
            } catch (JWTVerificationException e) {
                return null;
            }
        }
}
